import vnode from "./vnode.js";

// h 函数，返回虚拟节点
export default function (sel, data, c) {
  // 检查参数个数
  if (arguments.length !== 3) {
    throw new Error("参数必须为3个");
  }
  if (typeof c === "string" || typeof c === "number") {
    // 类型1
    return vnode(sel, data, undefined, c, undefined);
  } else if (Array.isArray(c)) {
    // 类型2
    let children = []
    for (let index = 0; index < c.length; index++) {
      if (!(typeof c[index] === "object" && c[index].hasOwnProperty("sel"))) {
        throw new Error("第三个参数类型错误1");
      }
      children.push(c[index])
    }
    return vnode(sel, data, children, undefined, undefined)
  } else if (typeof c === "object" && c.hasOwnProperty("sel")) {
    // 类型3
    let children = [c]
    return vnode(sel, data, children, undefined, undefined)
  } else {
    throw new Error("第三个参数类型错误");
  }
}
